<!DOCTYPE html><html class=split lang=en-US-x-hixie><script src=../link-fixup.js defer=""></script>
<!-- Mirrored from html.spec.whatwg.org/dev/worklets.html by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 10 Sep 2025 08:36:28 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<meta charset=utf-8><meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name=viewport><title>HTML Standard, Edition for Web Developers</title><meta content=#3c790a name=theme-color><meta content="light dark" name=color-scheme><link rel=stylesheet href=../../resources.whatwg.org/standard-shared-with-dev.css crossorigin=""><link rel=icon href=https://resources.whatwg.org/logo.svg crossorigin=""><link rel=stylesheet href=styles.css crossorigin=""><script>
   function toggleStatus(div) {
     div.parentNode.classList.toggle('wrapped');
   }
   function setLinkFragment(link) {
     link.hash = location.hash;
   }
  </script><body>
  <script async="" src=search.js></script>
  
  
  <header id=head class="head with-buttons">
   <a href=https://whatwg.org/ class=logo><img width=100 alt=WHATWG crossorigin="" class=darkmode-aware src=https://resources.whatwg.org/logo.svg height=100></a>
   
   <hgroup><h1><a rel=home href=index.html>HTML: The Living Standard</a></h1><p id=dev-edition-h2>Edition for Web Developers — Last Updated <span class=pubdate>10 September 2025</span></hgroup>
   

   <div id=search>
    <input placeholder="Search. Press '/'" autocomplete=off name=query id=query type=search>
    <ol id=results></ol>
   </div>
  </header>

  

  

  

  
  

  
  

  

  <nav><a href=workers.html>← 10 Web workers</a> — <a href=index.html>Table of Contents</a> — <a href=webstorage.html>12 Web storage →</a></nav><ol class=toc><li><a href=worklets.html#worklets><span class=secno>11</span> Worklets</a><ol><li><a href=worklets.html#worklets-intro><span class=secno>11.1</span> Introduction</a><ol><li><a href=worklets.html#worklets-motivations><span class=secno>11.1.1</span> Motivations</a><li><a href=worklets.html#worklets-idempotent><span class=secno>11.1.2</span> Code idempotence</a><li><a href=worklets.html#worklets-speculative><span class=secno>11.1.3</span> Speculative evaluation</a></ol><li><a href=worklets.html#worklets-examples><span class=secno>11.2</span> Examples</a><ol><li><a href=worklets.html#worklets-examples-loading><span class=secno>11.2.1</span> Loading scripts</a><li><a href=worklets.html#worklets-example-registering><span class=secno>11.2.2</span> Registering a class and invoking its methods</a></ol><li><a href=worklets.html#worklets-infrastructure><span class=secno>11.3</span> Infrastructure</a><ol><li><a href=worklets.html#worklets-global><span class=secno>11.3.1</span> The global scope</a><ol><li><a href=worklets.html#worklet-agents-and-event-loops><span class=secno>11.3.1.1</span> Agents and event loops</a></ol><li><a href=worklets.html#worklets-worklet><span class=secno>11.3.2</span> The <code>Worklet</code> class</a><li><a href=worklets.html#worklets-lifetime><span class=secno>11.3.3</span> The worklet's lifetime</a></ol></ol></ol><h2 id=worklets><span class=secno>11</span> Worklets<a href=#worklets class=self-link></a></h2>

  <h3 id=worklets-intro><span class=secno>11.1</span> Introduction<a href=#worklets-intro class=self-link></a></h3>

  

  <p>Worklets are a piece of specification infrastructure which can be used for running scripts
  independent of the main JavaScript execution environment, while not requiring any particular
  implementation model.</p>

  <p>The worklet infrastructure specified here cannot be used directly by web developers. Instead,
  other specifications build upon it to create directly-usable worklet types, specialized for
  running in particular parts of the browser implementation pipeline.</p>

  <h4 id=worklets-motivations><span class=secno>11.1.1</span> Motivations<a href=#worklets-motivations class=self-link></a></h4>

  

  <p>Allowing extension points to rendering, or other sensitive parts of the implementation pipeline
  such as audio output, is difficult. If extension points were done with full access to the APIs
  available on <code>Window</code>, engines would need to abandon previously-held assumptions for
  what could happen in the middle of those phases. For example, during the layout phase, rendering
  engines assume that no DOM will be modified.</p>

  <p>Additionally, defining extension points in the <code>Window</code> environment would restrict
  user agents to performing work in the same thread as the <code>Window</code> object. (Unless
  implementations added complex, high-overhead infrastructure to allow thread-safe APIs, as well
  as thread-joining guarantees.)</p>

  <p>Worklets are designed to allow extension points, while keeping guarantees that user agents
  currently rely on. This is done through new global environments, based on subclasses of
  <code id=worklets-motivations:workletglobalscope><a href=#workletglobalscope>WorkletGlobalScope</a></code>.</p>

  <p>Worklets are similar to web workers. However, they:</p>

  <ul><li><p>Are thread-agnostic. That is, they are not designed to run on a dedicated separate thread,
   like each worker is. Implementations can run worklets wherever they choose (including on the main
   thread).<li><p>Are able to have multiple duplicate instances of the global scope created, for the purpose
   of parallelism.<li><p>Do not use an event-based API. Instead, classes are registered on the global scope, whose
   methods are invoked by the user agent.<li><p>Have a reduced API surface on the global scope.<li><p>Have a lifetime for their <span>global object</span> which is defined by other
   specifications, often in an <a id=worklets-motivations:implementation-defined href=https://infra.spec.whatwg.org/#implementation-defined data-x-internal=implementation-defined>implementation-defined</a> manner.</ul>

  <p>As worklets have relatively high overhead, they are best used sparingly. Due to this, a given
  <code id=worklets-motivations:workletglobalscope-2><a href=#workletglobalscope>WorkletGlobalScope</a></code> is expected to be shared between multiple separate scripts. (This
  is similar to how a single <code>Window</code> is shared between multiple separate scripts.)</p>

  <p>Worklets are a general technology that serve different use cases. Some worklets, such as those
  defined in <cite>CSS Painting API</cite>, provide extension points intended for stateless,
  idempotent, and short-running computations, which have special considerations as described in the
  next couple of sections. Others, such as those defined in <cite>Web Audio API</cite>, are used for
  stateful, long-running operations. <a href=references.html#refsCSSPAINT>[CSSPAINT]</a> <a href=references.html#refsWEBAUDIO>[WEBAUDIO]</a></p>

  <h4 id=worklets-idempotent><span class=secno>11.1.2</span> Code idempotence<a href=#worklets-idempotent class=self-link></a></h4>

  <p>Some specifications which use worklets are intended to allow user agents to parallelize work
  over multiple threads, or to move work between threads as required. In these specifications, user
  agents might invoke methods on a web-developer-provided class in an
  <a id=worklets-idempotent:implementation-defined href=https://infra.spec.whatwg.org/#implementation-defined data-x-internal=implementation-defined>implementation-defined</a> order.</p>

  <p>As a result of this, to prevent interoperability issues, authors who register classes on such
  <code id=worklets-idempotent:workletglobalscope><a href=#workletglobalscope>WorkletGlobalScope</a></code>s should make their code idempotent. That is, a method or set of
  methods on the class should produce the same output given a particular input.</p>

  <p>This specification uses the following techniques in order to encourage authors to write code in
  an idempotent way:</p>

  <ul><li>
    <p>No reference to the global object is available (i.e., there is no counterpart to <code id=worklets-idempotent:dom-workerglobalscope-self><a href=workers.html#dom-workerglobalscope-self>self</a></code> on <code id=worklets-idempotent:workletglobalscope-2><a href=#workletglobalscope>WorkletGlobalScope</a></code>).</p>

    <p class=XXX>Although this was the intention when worklets were first specified, the
    introduction of <code>globalThis</code> has made it no longer true. See <a href=https://github.com/whatwg/html/issues/6059>issue #6059</a> for more discussion.</p>
   <li><p>Code is loaded as a <span>module script</span>, which results in the code being executed
   in strict mode and with no shared <code>this</code> referencing the global
   proxy.</ul>

  <p>Together, these restrictions help prevent two different scripts from sharing state using
  properties of the <span>global object</span>.</p>

  <p>Additionally, specifications which use worklets and intend to allow
  <a id=worklets-idempotent:implementation-defined-2 href=https://infra.spec.whatwg.org/#implementation-defined data-x-internal=implementation-defined>implementation-defined</a> behavior must obey the following:</p>

  <ul><li><p>They must require user agents to always have at least two <code id=worklets-idempotent:workletglobalscope-3><a href=#workletglobalscope>WorkletGlobalScope</a></code>
   instances per <code id=worklets-idempotent:worklet><a href=#worklet>Worklet</a></code>, and randomly assign a method or set of methods on a class to
   a particular <code id=worklets-idempotent:workletglobalscope-4><a href=#workletglobalscope>WorkletGlobalScope</a></code> instance. These specifications may provide an
   opt-out under memory constraints.<li><p>These specifications must allow user agents to create and destroy instances of their
   <code id=worklets-idempotent:workletglobalscope-5><a href=#workletglobalscope>WorkletGlobalScope</a></code> subclasses at any time.</ul>

  <h4 id=worklets-speculative><span class=secno>11.1.3</span> Speculative evaluation<a href=#worklets-speculative class=self-link></a></h4>

  <p>Some specifications which use worklets can invoke methods on a web-developer-provided class
  based on the state of the user agent. To increase concurrency between threads, a user agent may
  invoke a method speculatively, based on potential future states.</p>

  <p>In these specifications, user agents might invoke such methods at any time, and with any
  arguments, not just ones corresponding to the current state of the user agent. The results of such
  speculative evaluations are not displayed immediately, but can be cached for use if the user agent
  state matches the speculated state. This can increase the concurrency between the user agent and
  worklet threads.</p>

  <p>As a result of this, to prevent interoperability risks between user agents, authors who
  register classes on such <code id=worklets-speculative:workletglobalscope><a href=#workletglobalscope>WorkletGlobalScope</a></code>s should make their code stateless. That
  is, the only effect of invoking a method should be its result, and not any side effects such as
  updating mutable state.</p>

  <p>The same techniques which encourage <a href=#worklets-idempotent>code idempotence</a> also
  encourage authors to write stateless code.</p>

  <div class=non-normative>

  <h3 id=worklets-examples><span class=secno>11.2</span> Examples<a href=#worklets-examples class=self-link></a></h3>

  

  <p>For these examples, we'll use a fake worklet. The <code>Window</code> object provides two
  <code id=worklets-examples:worklet><a href=#worklet>Worklet</a></code> instances, which each run code in their own collection of
  <code id=worklets-examples:fakeworkletglobalscope><a href=#fakeworkletglobalscope>FakeWorkletGlobalScope</a></code>s:

  

  <dl class=domintro><dt><code>window.fakeWorklet1</code><dd>Returns one of the fake worklets.<dt><code>window.fakeWorklet2</code><dd>Returns another of the fake worklets.</dl>

  <p>Both of these have their <a href=#worklet-global-scope-type id=worklets-examples:worklet-global-scope-type>worklet
  global scope type</a> set to <code id=worklets-examples:fakeworkletglobalscope-2><a href=#fakeworkletglobalscope>FakeWorkletGlobalScope</a></code>, and their <a href=#worklet-destination-type id=worklets-examples:worklet-destination-type>worklet
  destination type</a> set to "<code>fakeworklet</code>". User agents should create at
  least two <code id=worklets-examples:fakeworkletglobalscope-3><a href=#fakeworkletglobalscope>FakeWorkletGlobalScope</a></code> instances per worklet.</p>

  <p class=note>"<code>fakeworklet</code>" is not actually a valid <a href=https://fetch.spec.whatwg.org/#concept-request-destination id=worklets-examples:concept-request-destination data-x-internal=concept-request-destination>destination</a> per <cite>Fetch</cite>. But this
  illustrates how real worklets would generally have their own worklet-type-specific destination.
  <a href=references.html#refsFETCH>[FETCH]</a></p>

  

  <hr>

  

  <p>Inside a <code id=fakeworkletglobalscope>FakeWorkletGlobalScope</code>, the following global method is
  available:</p>

  <dl class=domintro><dt><code>registerFake(<var>type</var>, <var>classConstructor</var>)</code><dd>Registers the JavaScript class given by <var>classConstructor</var> for use when the user
   agent later wants to do some operation specified by <var>type</var>.</dl>

  

  <h4 id=worklets-examples-loading><span class=secno>11.2.1</span> Loading scripts<a href=#worklets-examples-loading class=self-link></a></h4>

  

  <p>To load scripts into <span>fake worklet 1</span>, a web developer would write:</p>

  <pre><code class='js'>window<c- p>.</c->fakeWorklet1<c- p>.</c->addModule<c- p>(</c-><c- t>&apos;script1.mjs&apos;</c-><c- p>);</c->
window<c- p>.</c->fakeWorklet1<c- p>.</c->addModule<c- p>(</c-><c- t>&apos;script2.mjs&apos;</c-><c- p>);</c-></code></pre>

  <p>Note that which script finishes fetching and runs first is dependent on network timing: it
  could be either <code>script1.mjs</code> or <code>script2.mjs</code>. This
  generally won't matter for well-written scripts intended to be loaded in worklets, if they follow
  the suggestions about preparing for <a href=#worklets-speculative>speculative
  evaluation</a>.</p>

  <p>If a web developer wants to perform a task only after the scripts have successfully run and
  loaded into some worklets, they could write:</p>

  <pre><code class='js'>Promise<c- p>.</c->all<c- p>([</c->
    window<c- p>.</c->fakeWorklet1<c- p>.</c->addModule<c- p>(</c-><c- t>&apos;script1.mjs&apos;</c-><c- p>),</c->
    window<c- p>.</c->fakeWorklet2<c- p>.</c->addModule<c- p>(</c-><c- t>&apos;script2.mjs&apos;</c-><c- p>)</c->
<c- p>]).</c->then<c- p>(()</c-> <c- p>=&gt;</c-> <c- p>{</c->
    <c- c1>// Do something which relies on those scripts being loaded.</c->
<c- p>});</c-></code></pre>

  <hr>

  <p>Another important point about script-loading is that loaded scripts can be run in multiple
  <code id=worklets-examples-loading:workletglobalscope><a href=#workletglobalscope>WorkletGlobalScope</a></code>s per <code id=worklets-examples-loading:worklet><a href=#worklet>Worklet</a></code>, as discussed in the section on <a href=#worklets-idempotent>code idempotence</a>. In particular, the specification above
  for <span>fake worklet 1</span> and <span>fake worklet 2</span> require this. So, consider a
  scenario such as the following:</p>

  <pre><code class='js'><c- c1>// script.mjs</c->
console<c- p>.</c->log<c- p>(</c-><c- u>&quot;Hello from a FakeWorkletGlobalScope!&quot;</c-><c- p>);</c-></code></pre>

  <pre><code class='js'><c- c1>// app.mjs</c->
window<c- p>.</c->fakeWorklet1<c- p>.</c->addModule<c- p>(</c-><c- u>&quot;script.mjs&quot;</c-><c- p>);</c-></code></pre>

  <p>This could result in output such as the following from a user agent's console:</p>

  <pre><code>[fakeWorklet1#1] Hello from a FakeWorkletGlobalScope!
[fakeWorklet1#4] Hello from a FakeWorkletGlobalScope!
[fakeWorklet1#2] Hello from a FakeWorkletGlobalScope!
[fakeWorklet1#3] Hello from a FakeWorkletGlobalScope!</code></pre>

  <p>If the user agent at some point decided to kill and restart the third instance of
  <code id=worklets-examples-loading:fakeworkletglobalscope><a href=#fakeworkletglobalscope>FakeWorkletGlobalScope</a></code>, the console would again print <code>[fakeWorklet1#3] Hello from a FakeWorkletGlobalScope!</code> when this occurs.</p>

  <h4 id=worklets-example-registering><span class=secno>11.2.2</span> Registering a class and invoking its methods<a href=#worklets-example-registering class=self-link></a></h4>

  

  <p>Let's say that one of the intended usages of our fake worklet by web developers is to allow
  them to customize the highly-complex process of boolean negation. They might register their
  customization as follows:</p>

  <pre><code class='js'><c- c1>// script.mjs</c->
registerFake<c- p>(</c-><c- t>&apos;negation-processor&apos;</c-><c- p>,</c-> <c- a>class</c-> <c- p>{</c->
  process<c- p>(</c->arg<c- p>)</c-> <c- p>{</c->
    <c- k>return</c-> <c- o>!</c->arg<c- p>;</c->
  <c- p>}</c->
<c- p>});</c-></code></pre>

  <pre><code class='js'><c- c1>// app.mjs</c->
window<c- p>.</c->fakeWorklet1<c- p>.</c->addModule<c- p>(</c-><c- u>&quot;script.mjs&quot;</c-><c- p>);</c-></code></pre>

  <p>To make use of such registered classes, the specification for fake worklets could define a <dfn id=fakeworkletglobalscope-process>find the opposite of true</dfn> algorithm, given a
  <code id=worklets-example-registering:worklet><a href=#worklet>Worklet</a></code> <var>worklet</var><span>, which invokes the <code>process</code> method on any class registered to one of <var>worklet</var>'s global
  scopes as having type "<code>negation-processor</code>", with true as the argument, and
  then uses the result in some way.</span></p>

  

  

  </div>

  <h3 id=worklets-infrastructure><span class=secno>11.3</span> Infrastructure<a href=#worklets-infrastructure class=self-link></a></h3>

  <h4 id=worklets-global><span class=secno>11.3.1</span> The global scope<a href=#worklets-global class=self-link></a></h4>

  <p>Subclasses of <code id=workletglobalscope>WorkletGlobalScope</code> are used to create <span>global objects</span> wherein code loaded into a particular <code id=worklets-global:worklet><a href=#worklet>Worklet</a></code> can
  execute.</p>

  

  <p class=note>Other specifications are intended to subclass <code id=worklets-global:workletglobalscope><a href=#workletglobalscope>WorkletGlobalScope</a></code>,
  adding APIs to register a class, as well as other APIs specific for their worklet type.</p>

  

  <h5 id=worklet-agents-and-event-loops><span class=secno>11.3.1.1</span> Agents and event loops<a href=#worklet-agents-and-event-loops class=self-link></a></h5>

  

  <p>Each <code id=worklet-agents-and-event-loops:workletglobalscope><a href=#workletglobalscope>WorkletGlobalScope</a></code> is contained in its own <a id=worklet-agents-and-event-loops:worklet-agent href=webappapis.html#worklet-agent>worklet agent</a>, which
  has its corresponding <a href=webappapis.html#concept-agent-event-loop id=worklet-agents-and-event-loops:concept-agent-event-loop>event loop</a>. However, in
  practice, implementation of these agents and event loops is expected to be different from most
  others.</p>

  <p>A <a id=worklet-agents-and-event-loops:worklet-agent-2 href=webappapis.html#worklet-agent>worklet agent</a> exists for each <code id=worklet-agents-and-event-loops:workletglobalscope-2><a href=#workletglobalscope>WorkletGlobalScope</a></code> since, in theory,
  an implementation could use a separate thread for each <code id=worklet-agents-and-event-loops:workletglobalscope-3><a href=#workletglobalscope>WorkletGlobalScope</a></code> instance,
  and allowing this level of parallelism is best done using agents. However, because their
  [[CanBlock]] value is false, there is no requirement that agents and threads are one-to-one. This
  allows implementations the freedom to execute scripts loaded into a worklet on any thread,
  including one running code from other agents with [[CanBlock]] of false, such as the thread of a
  <a id=worklet-agents-and-event-loops:similar-origin-window-agent href=webappapis.html#similar-origin-window-agent>similar-origin window agent</a> ("the main thread"). Contrast this with <a href=webappapis.html#dedicated-worker-agent id=worklet-agents-and-event-loops:dedicated-worker-agent>dedicated worker agents</a>, whose true value for [[CanBlock]]
  effectively requires them to get a dedicated operating system thread.</p>

  <p>Worklet <a href=webappapis.html#event-loop id=worklet-agents-and-event-loops:event-loop>event loops</a> are also somewhat special. They are only
  used for <span>tasks</span> associated with <code id=worklet-agents-and-event-loops:dom-worklet-addmodule><a href=#dom-worklet-addmodule>addModule()</a></code>, tasks wherein the user agent invokes
  author-defined methods, and <span>microtasks</span>. Thus, even though the <a href=#event-loop-processing-model>event loop processing model</a> specifies that all event loops
  run continuously, implementations can achieve observably-equivalent results using a simpler
  strategy, which just <a href=https://webidl.spec.whatwg.org/#invoke-a-callback-function id=worklet-agents-and-event-loops:es-invoking-callback-functions data-x-internal=es-invoking-callback-functions>invokes</a> author-provided
  methods and then relies on that process to <span>perform a microtask checkpoint</span>.</p>

  

  <h4 id=worklets-worklet><span class=secno>11.3.2</span> The <code id=worklets-worklet:worklet><a href=#worklet>Worklet</a></code> class<a href=#worklets-worklet class=self-link></a></h4><div class="mdn-anno wrapped"><button onclick=toggleStatus(this) class=mdn-anno-btn><b title="Support in all current engines." class=all-engines-flag>✔</b><span>MDN</span></button><div class=feature><p><a href=https://developer.mozilla.org/en-US/docs/Web/API/Worklet title="The Worklet interface is a lightweight version of Web Workers and gives developers access to low-level parts of the rendering pipeline.">Worklet</a><p class=all-engines-text>Support in all current engines.<div class=support><span class="firefox yes"><span>Firefox</span><span>76+</span></span><span class="safari yes"><span>Safari</span><span>14.1+</span></span><span class="chrome yes"><span>Chrome</span><span>65+</span></span><hr><span class="opera unknown"><span>Opera</span><span>?</span></span><span class="edge_blink yes"><span>Edge</span><span>79+</span></span><hr><span class="edge unknown"><span>Edge (Legacy)</span><span>?</span></span><span class="ie no"><span>Internet Explorer</span><span>No</span></span><hr><span class="firefox_android unknown"><span>Firefox Android</span><span>?</span></span><span class="safari_ios unknown"><span>Safari iOS</span><span>?</span></span><span class="chrome_android unknown"><span>Chrome Android</span><span>?</span></span><span class="webview_android unknown"><span>WebView Android</span><span>?</span></span><span class="samsunginternet_android unknown"><span>Samsung Internet</span><span>?</span></span><span class="opera_android unknown"><span>Opera Android</span><span>?</span></span></div></div></div>

  <p>The <code id=worklet>Worklet</code> class provides the capability to add module scripts into its
  associated <code id=worklets-worklet:workletglobalscope><a href=#workletglobalscope>WorkletGlobalScope</a></code>s. The user agent can then create classes registered on
  the <code id=worklets-worklet:workletglobalscope-2><a href=#workletglobalscope>WorkletGlobalScope</a></code>s and invoke their methods.</p>

  

  <p>Specifications that create <code id=worklets-worklet:worklet-2><a href=#worklet>Worklet</a></code> instances must specify the following for a
  given instance:</p>

  <ul><li><p>its <dfn id=worklet-global-scope-type data-export="">worklet global scope type</dfn>, which must be a Web IDL type that <a href=https://webidl.spec.whatwg.org/#dfn-inherit id=worklets-worklet:inherit data-x-internal=inherit>inherits</a> from <code id=worklets-worklet:workletglobalscope-3><a href=#workletglobalscope>WorkletGlobalScope</a></code>; and<li><p>its <dfn id=worklet-destination-type data-export="">worklet destination type</dfn>, which must be a <a href=https://fetch.spec.whatwg.org/#concept-request-destination id=worklets-worklet:concept-request-destination data-x-internal=concept-request-destination>destination</a>, and is used when fetching
   scripts.</ul>

  <dl class=domintro><dt><code>await <var>worklet</var>.<span id=dom-worklet-addmodule>addModule</span>(<var>moduleURL</var>[, { <span>credentials</span> }])</code><dd>
    <p>Loads and executes the <span>module script</span> given by <var>moduleURL</var> into all of
    <var>worklet</var>'s <span>global scopes</span>. It can
    also create additional global scopes as part of this process, depending on the worklet type. The
    returned promise will fulfill once the script has been successfully loaded and run in all global
    scopes.</p>

    <p>The <code>credentials</code> option can be set to a
    <a href=https://fetch.spec.whatwg.org/#concept-request-credentials-mode id=worklets-worklet:concept-request-credentials-mode data-x-internal=concept-request-credentials-mode>credentials mode</a> to modify the
    script-fetching process. It defaults to "<code>same-origin</code>".</p>

    <p>Any failures in <span>fetching</span> the script or its
    dependencies will cause the returned promise to be rejected with an
    <a id=worklets-worklet:aborterror href=https://webidl.spec.whatwg.org/#aborterror data-x-internal=aborterror>"<code>AbortError</code>"</a> <code id=worklets-worklet:domexception><a data-x-internal=domexception href=https://webidl.spec.whatwg.org/#dfn-DOMException>DOMException</a></code>. Any errors in parsing the
    script or its dependencies will cause the returned promise to be rejected with the exception
    generated during parsing.</p>
   </dl>

  

  <h4 id=worklets-lifetime><span class=secno>11.3.3</span> The worklet's lifetime<a href=#worklets-lifetime class=self-link></a></h4>

  <p>The lifetime of a <code id=worklets-lifetime:worklet><a href=#worklet>Worklet</a></code> has no special considerations; it is tied to the object
  it belongs to, such as the <code>Window</code>.</p>

  

  <p>The lifetime of a <code id=worklets-lifetime:workletglobalscope><a href=#workletglobalscope>WorkletGlobalScope</a></code> is, at a minimum, tied to the
  <code>Document</code> whose <span>worklet global
  scopes</span> contain it. In particular, <a href=document-lifecycle.html#destroy-a-document id=worklets-lifetime:destroy-a-document>destroying</a> the
  <code>Document</code> will <span>terminate</span> the
  corresponding <code id=worklets-lifetime:workletglobalscope-2><a href=#workletglobalscope>WorkletGlobalScope</a></code> and allow it to be garbage-collected.</p>

  <p>Additionally, user agents may, at any time, <span>terminate</span> a given <code id=worklets-lifetime:workletglobalscope-3><a href=#workletglobalscope>WorkletGlobalScope</a></code>, unless the specification defining
  the corresponding worklet type says otherwise. For example, they might terminate them if the
  <a id=worklets-lifetime:worklet-agent href=webappapis.html#worklet-agent>worklet agent</a>'s <a href=webappapis.html#concept-agent-event-loop id=worklets-lifetime:concept-agent-event-loop>event loop</a> has no
  <span>tasks</span> queued, or if the user agent has no pending operations
  planning to make use of the worklet, or if the user agent detects abnormal operations such as
  infinite loops or callbacks exceeding imposed time limits.</p>

  <p>Finally, specifications for specific worklet types can give more specific details on when to
  <span>create</span> <code id=worklets-lifetime:workletglobalscope-4><a href=#workletglobalscope>WorkletGlobalScope</a></code>s for a
  given worklet type. For example, they might create them during specific processes that call upon
  worklet code, as in the <a href=#worklets-example-registering>example</a>.</p>

  <nav><a href=workers.html>← 10 Web workers</a> — <a href=index.html>Table of Contents</a> — <a href=webstorage.html>12 Web storage →</a></nav>
